﻿/// <reference path="/Scripts/jquery-1.5.1.min.js">
///<reference path="/Scripts/jQuery.tmpl.min.js"></script>
var pageHandler = function (option,orderby) {

    this.pageIndex = 0;
    this.option = option;
    this.orderby = orderby;
    var self = this;

    $("#" + option.pageID + " .divPreviousPage").live("click", function () {
        if ($(this).hasClass("button_nenable")) {
            return false;
        }
        else {
            self.pageIndex -= 1;
            self.dataLoad(self.pageIndex);
        }
    })
    $("#" + option.pageID + " .divNextPage").live("click", function () {
        if ($(this).hasClass("button_nenable")) {
            return false;
        }
        else {
            self.pageIndex += 1;
            self.dataLoad(self.pageIndex);
        }
    })
    if (this.option.isCss) {
        $("#" + this.option.contentID + ">div").live({
            mouseenter: function () {
                $(this).addClass("hoverbackcolor");
            }, mouseleave: function () {
                $(this).removeClass("hoverbackcolor");
            }
        })
    }
}
pageHandler.prototype = {
    pageInit: function () {
        $("#" + this.option.pageID).html('<div style=" margin:10px auto; height:20px;width:240px;clear:both;">'
                                                    + '<div  class="divPreviousPage button_enable">上一页</div>'
                                                    + '<div  class="divPageDesc" style="float:left; width:100px; height:20px;text-align:center;"></div>'
                                                    + '<div  class="divNextPage button_enable" >下一页</div></div>');
        this.pageIndex = 0;
        this.dataLoad(0);
    },

    dataLoad: function (index) {

        var obj = "g=" + $("#" + this.option.queryID).val().trim() + "&PageIndex=" + index;
        if (this.orderby != null && this.orderby != undefined)
        {
            obj += "&orderby=" + this.orderby;
        }
        var s = this.option;
        $.getJSON(this.option.url, encodeURI(obj), function (data) {
            $("#" + s.contentID).html("");
            $("#" + s.tmplID).tmpl(data.ItemList).appendTo("#" + s.contentID);
            var divPreviousPage = $("#" + s.pageID + " .divPreviousPage");
            var divNextPage = $("#" + s.pageID + " .divNextPage");
            var divPageDesc = $("#" + s.pageID + " .divPageDesc");
            if (data.TotalPages > 0) {
                divPageDesc.text("第" + (index + 1).toString() + "页" + ",共" + data.TotalPages + "页");
            }
            if (data.HasPreviousPage && data.HasNextPage) {
                divPreviousPage.removeClass("button_nenable");
                divPreviousPage.addClass("button_enable")
                divNextPage.removeClass("button_nenable");
                divNextPage.addClass("button_enable");
            }
            else if (!data.HasPreviousPage && data.HasNextPage) {
                divPreviousPage.removeClass("button_enable");
                divPreviousPage.addClass("button_nenable");
                divNextPage.removeClass("button_nenable");
                divNextPage.addClass("button_enable");
            }
            else if (data.HasPreviousPage && !data.HasNextPage) {
                divPreviousPage.removeClass("button_nenable");
                divPreviousPage.addClass("button_enable")
                divNextPage.removeClass("button_enable");
                divNextPage.addClass("button_nenable");
            }
            else {
                divPreviousPage.removeClass("button_enable");
                divPreviousPage.addClass("button_nenable")
                divNextPage.removeClass("button_enable");
                divNextPage.addClass("button_nenable");
            }

            if (s.isCss) {
                //$("#" + s.contentID + ">div:even").addClass("evenbackcolor");
            }
        });
    }
}